﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DNA.Mvc.UI
{
    public static class LayoutSwitcher
    {
        public static string GetMasterLayout(string layout = "_Layout.cshtml")
        {
            var web = WebSiteContext.Current.Web;

            if (!string.IsNullOrEmpty(layout))
            {
                if (WebSiteContext.Current.Web != null && System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/content/themes/" + web.Theme + "/"+layout)))
                {
                    return "~/content/themes/" + WebSiteContext.Current.Web.Theme + "/"+layout;
                }
                else
                {
                    return "~/Views/Shared/"+layout;
                }
            }
            return "";
        }

        public static string GetLayoutView(string viewName,string area="")
        {
            var web = WebSiteContext.Current.Web;

            if (!string.IsNullOrEmpty(viewName))
            {
                if (WebSiteContext.Current.Web != null && System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/content/themes/" + web.Theme + "/"+ (string.IsNullOrEmpty(area) ? "" : (area+"/")) + viewName+".cshtml")))
                {
                    return "~/content/themes/" + WebSiteContext.Current.Web.Theme + "/" + (string.IsNullOrEmpty(area) ? "" : (area + "/")) + viewName + ".cshtml";
                }
                else
                {
                    return viewName;
                }
            }
            return "";
        }

    }
}
